//2105.给植物浇水Ⅱ
//https://leetcode.cn/problems/watering-plants-ii
class Solution {
public:
    int minimumRefill(vector<int>& plants, int capacityA, int capacityB) {
        int n = plants.size() - 1;
        int p1 = 0, p2 = n;
        int capA = capacityA, capB = capacityB;
        int ret = 0;
        while (p1 <= p2)
        {
            if (p1 == p2)
            {
                if(capA < plants[p1] && capB < plants[p2]) ++ret;
                --p2, ++p1;
            }
            else
            {
                if (capA < plants[p1])
                {
                    ++ret;
                    capA = capacityA;
                }
                capA -= plants[p1++];

                if (capB < plants[p2])
                {
                    ++ret;
                    capB = capacityB;
                }
                capB -= plants[p2--];
            }
        }
        return ret;
    }
};